package com.farmpost.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmpost.server.mapper.UserMaterialsMapper;
import com.farmpost.server.pojo.RespBean;
import com.farmpost.server.pojo.UserInfo;
import com.farmpost.server.pojo.UserMaterials;
import com.farmpost.server.service.IUserMaterialsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户相关资料表 服务实现类
 * </p>
 *
 * @author king
 * @since 2022-04-03
 */
@Service
public class UserMaterialsServiceImpl extends ServiceImpl<UserMaterialsMapper, UserMaterials> implements IUserMaterialsService {

    @Autowired
    private UserMaterialsMapper userMaterialsMapper;
    /**
     * 获取用户资料
     * @return
     */
    @Override
    public UserMaterials getUSerMaterials() {
        UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserMaterials materials = userMaterialsMapper.selectOne(new QueryWrapper<UserMaterials>().eq("user_id", user.getUserId()));
        return materials;
    }

    /**
     * 修改用户资料
     * @param isStuCertification
     * @param selfIntroduction
     * @param greatCount
     * @param myFanCount
     * @param myFocusCount
     * @return
     */
    @Override
    public RespBean updateUserMaterials(Integer isStuCertification, String selfIntroduction, Integer greatCount, Integer myFanCount, Integer myFocusCount) {
        UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserMaterials userMaterials = new UserMaterials();
        userMaterials.setUserId(user.getUserId());
        userMaterials.setIsStuCertification(isStuCertification);
        userMaterials.setSelfIntroduction(selfIntroduction);
        userMaterials.setGreatCount(greatCount);
        userMaterials.setMyFanCount(myFanCount);
        userMaterials.setMyFocusCount(myFocusCount);
        int update = userMaterialsMapper.update(userMaterials, new UpdateWrapper<UserMaterials>().eq("user_id", user.getUserId()));
        if (update == 1)
            return RespBean.success("修改用户资料成功");
        return RespBean.error("修改用户资料失败");
    }

    /**
     * 插入用户资料
     * @param userMaterials
     * @return
     */
    @Override
    public RespBean insertUserMaterials(UserMaterials userMaterials) {
        int insert = userMaterialsMapper.insert(userMaterials);
        if (insert == 1)
            return RespBean.success("插入资料成功");
        return RespBean.error("插入资料失败");
    }

    /**
     * 删除用户资料
     * @param userId
     * @return
     */
    @Override
    public RespBean deleteUserMaterials(Integer userId) {
        int delete = userMaterialsMapper.delete(new QueryWrapper<UserMaterials>().eq("user_id", userId));
        if (delete == 1)
            return RespBean.success("用户资料删除成功");
        return RespBean.error("用户资料删除失败");
    }
}
